/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetotcc2;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author FoSa
 * Classe para abilitar drop de arquivos no programa
 *
 */
public class dropFile implements DropTargetListener, PropertyChangeListener {

    protected DropTarget dropTarget;
    protected ProjetoTCC2View mainwindow;

    public dropFile(JPanel jPanel4, ProjetoTCC2View main) {
        mainwindow = main;
        dropTarget = new DropTarget(jPanel4,
                DnDConstants.ACTION_COPY_OR_MOVE,
                this,
                true, null);
    }

    public void dragEnter(DropTargetDragEvent dtde) {
    }

    public void dragOver(DropTargetDragEvent dtde) {
    }

    public void dropActionChanged(DropTargetDragEvent dtde) {
    }

    public void dragExit(DropTargetEvent dte) {
    }

    public void drop(DropTargetDropEvent dtde) {
        if ((dtde.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) != 0) {
            dtde.acceptDrop(dtde.getDropAction());

            List files = null;
            Transferable transferable = dtde.getTransferable();
            try {
                if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                    files = (List) transferable.getTransferData(DataFlavor.javaFileListFlavor);
                    for (int i = 0; i < files.size(); i++) {
                        mainwindow.openFile(new File(files.get(i).toString()));
                    }
                } else if (transferable.isDataFlavorSupported(new DataFlavor("text/uri-list;class=java.lang.String"))) {
                    String sti = (String) transferable.getTransferData(new DataFlavor("text/uri-list;class=java.lang.String"));
                    StringTokenizer tokien = new StringTokenizer(sti, "\r\n");
                    while (tokien.hasMoreTokens()) {
                        String line = tokien.nextToken();
                        try {
                            URI uri = new URI(line);
                            File file = new File(uri);
                            mainwindow.openFile(file);
                        } catch (Exception ex) {
                            Logger.getLogger(dropFile.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                } else if (transferable.isDataFlavorSupported(new DataFlavor("application/x-java-serialized-object; class=java.lang.String"))) {
                    String sti = (String) transferable.getTransferData(new DataFlavor("application/x-java-serialized-object; class=java.lang.String"));
                    StringTokenizer tokien = new StringTokenizer(sti, "\r\n");
                    while (tokien.hasMoreTokens()) {
                        String line = tokien.nextToken();
                        try {
                            URI uri = new URI(line.replaceAll(" ", "%20").replaceAll("ç", "%C3").replaceAll("ã", "%A3"));
                            File file = new File(uri);
                            mainwindow.openFile(file);
                        } catch (Exception ex) {
                            Logger.getLogger(dropFile.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                } else if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    String sti = (String) transferable.getTransferData(DataFlavor.stringFlavor);
                    StringTokenizer tokien = new StringTokenizer(sti, "\r\n");
                    while (tokien.hasMoreTokens()) {
                        String line = tokien.nextToken();
                        try {
                            URI uri = new URI(line.replaceAll(" ", "%20").replaceAll("ç", "%C3").replaceAll("ã", "%A3"));
                            File file = new File(uri);
                            mainwindow.openFile(file);
                        } catch (Exception ex) {
                            Logger.getLogger(dropFile.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(dropFile.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedFlavorException ex) {
                Logger.getLogger(dropFile.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(dropFile.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void propertyChange(PropertyChangeEvent evt) {
    }
}
